import { formatDate, rangeOfMonth, rangeOfWeek, wholeYear, dayTimeStart, dayTimeEnd } from 'pub-bbx-utils'
import { safeNewDate } from '@src/util/time';

const weekList = ['average-response-time', 'average-service-time', 'average-maintenance-time', 'timely-closing-rate']
const monthList = ['revenue-expenditure-analysis', 'spare-part-sale'] // 获取本月数据

/** 
 * @description  init图表搜索时间
*/
export function userChartInitDate(chartType: string){

	if (monthList.includes(chartType)) {
		const [monthStartDate, monthEndDate] = rangeOfMonth(safeNewDate())
		return {
			startTime: +dayTimeStart(monthStartDate),
			endTime: +dayTimeEnd(monthEndDate),
		}
	}
  
	if (weekList.includes(chartType)) {
		const [monthStartDate, monthEndDate] = rangeOfWeek(safeNewDate())
		return {
			startTime: +dayTimeStart(monthStartDate),
			endTime: +dayTimeEnd(monthEndDate),
		}
	}
  
	const [yearStartDate, yearEndDate] = wholeYear(safeNewDate())
	
  return{ 
		startTime: +dayTimeStart(yearStartDate),
		endTime: +dayTimeEnd(yearEndDate),
  }
}

/** 
 * @description  获取本月时间
*/
function setCurrentMonthDate(){
	let curDate = safeNewDate()
	let y = curDate.getFullYear()
	let m = curDate.getMonth() + 1
	if( m > 12 ){
			m = 1;
			y++ 
	 }
	let monthLastDay = safeNewDate(y,m,0).getDate()
	const startTime =  y + '-' + (m < 10 ? '0'+m : m) + '-' + '01'
	const endTime =  y + '-' + (m < 10 ? '0'+m : m) + '-' + (monthLastDay < 10 ? '0'+monthLastDay : monthLastDay)

	return {
		startTime,
		endTime
	}
}

/** 
 * @description  设置时间周期默认值
*/
export function getDefaultDimension(chart: string) {
	if (weekList.findIndex(item => item === chart) > -1) return 1
	return 2
}